# == Schema Information
# Schema version: 22
#
# Table name: comments
#
#  id               :integer       not null, primary key
#  body             :text          default("")
#  created_at       :datetime      not null
#  commentable_id   :integer       default(0), not null
#  commentable_type :string(15)    default(""), not null
#  user_id          :integer       default(0), not null
#

class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true
  belongs_to :user
  validates_presence_of :user, :body
  after_create :update_cached_fields

  protected

  def update_cached_fields
    self.commentable_type.constantize.update_all(['replied_at = ?', created_at], ['id = ?', commentable_id])
  end

end
